//
//  Head_2VIew.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "Head_2VIew.h"

#import "LRAFNetwork.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "TopicModel.h"
#import "Head_2TableViewCell.h"
#import "TuijianViewController.h"
#import "JinrituijianViewController.h"
@implementation Head_2VIew
- (void)dealloc
{
    [_dataArr release];
    [_title release];
    [super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self =  [super initWithFrame:frame];
    if (self) {
        self.title = [[ UILabel alloc] initWithFrame:CGRectMake(0*WIDTH, HEIGHT*0,  WIDTH*230, HEIGHT*40)];
        self.title.backgroundColor = [UIColor whiteColor];
        self.title.textAlignment = NSTextAlignmentRight;
        self.title.text = @"#今日推荐#";
        self.btn =[ UIButton buttonWithType:UIButtonTypeSystem];
        _btn.frame = CGRectMake( 375 * WIDTH-(70*WIDTH), 0, WIDTH*60, _title.frame.size.height);
        [_btn setTitle:@"更多  >" forState:UIControlStateNormal];
        self.btn.backgroundColor = [UIColor whiteColor];
        [self.btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        //[self.btn setBackgroundImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
        [self addSubview:_btn];
        [self addSubview:self.title];
        [self creatTableview];
        [self post];
    }
    return self;
}

-(void)btnAction:(UIButton *)b
{
    NSLog(@"111");
    [self.btndelegate passValue];
    
    
}
-(void)post
{
    NSString *str = @"http://api.meishi.cc/v5/index3.php?format=json?lon=&source=android&lat=&c_md5=b79c4bb9d8b83017dcaea6fff3ca35c8&format=json&";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *arr = responseObject[@"obj"][@"zt"];
        self.dataArr = [TopicModel modelHanderWithArray:arr];
        NSLog(@"!!!!!!%@",_dataArr);
        [tableview reloadData];
        //[tableview reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
-(void)creatTableview
{
    tableview =[[ UITableView alloc] initWithFrame:CGRectMake(0, 40 *HEIGHT, 375 *WIDTH, HEIGHT*600) style:UITableViewStylePlain] ;
    tableview.backgroundColor = [ UIColor clearColor];
    tableview.dataSource = self;
    tableview.delegate = self;
    [self addSubview:tableview];
    [tableview registerClass:[ Head_2TableViewCell class] forCellReuseIdentifier:@"cell"];
  
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Head_2TableViewCell *cell = [ tableview dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
   TopicModel *m = _dataArr[ indexPath.row];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.photo]];
    
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200 * HEIGHT;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ TopicModel *t = _dataArr[indexPath.row];
    [self.delegate passWithURL:t.f_s_type];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
